/**
 * 
 */
package com.qdairlines.controller.dict;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qdairlines.common.ControllerUtil;
import com.qdairlines.constant.BaseConsts;
import com.qdairlines.constant.DictionaryConsts;
import com.qdairlines.constant.RequestConsts;
import com.qdairlines.controller.base.BaseController;
import com.qdairlines.entity.dict.DictionaryItem;
import com.qdairlines.service.dict.DictionaryItemService;
import com.qdairlines.service.dict.DictionaryService;

/**
 * @author pangyannan
 * @date 2017年3月6日
 * @company QingDao Airlines
 * @description 字典明细控制器
 */
@Controller
@RequestMapping("dictItem")
public class DictionaryItemController extends BaseController{
	@Autowired
	private DictionaryItemService dictionaryItemService;
	
	
	@Autowired
	private DictionaryService dictionaryService;
	
	
	/**
	 * 获取所有的字典明细
	 * @param request
	 * @param dictionaryItem
	 * @return
	 */
    @RequestMapping(value = "listDictionaryItem",produces = RequestConsts.PRODUCES)
    @ResponseBody
    public String listDictionaryItem(HttpServletRequest request,DictionaryItem dictionaryItem) {
    	List<DictionaryItem> list  = dictionaryItemService.listData(dictionaryItem);

    	dictionaryService.convertDictValueToText(list, "status", "statusName", DictionaryConsts.DICTIONART_ITEM_STATUS, ControllerUtil.getLanguage(request));
    	return ControllerUtil.getJson(list);
    }
    
    /**
     * 保存字典明细
     * @param request
     * @param dictionary
     * @return
     */
    @RequestMapping(value = "save",produces = RequestConsts.PRODUCES)
    @ResponseBody
    @Transactional
    public String save(HttpServletRequest request,DictionaryItem dictionaryItem) { 
    	//检查编码是否重复
		DictionaryItem dictionaryItemCk  = new DictionaryItem();
		dictionaryItemCk.setItemValue(dictionaryItem.getItemValue());
		dictionaryItemCk.setDictionaryId(dictionaryItem.getDictionaryId());
		List<DictionaryItem> list = dictionaryItemService.listData(dictionaryItemCk);
		if(list != null && list.size() > 0 ){
			for(DictionaryItem d: list){
				//排除自己
				if(d.getId().equals(dictionaryItem.getId())){
					continue;
				}
				if(dictionaryItem.getItemValue().equals(d.getItemValue())){
					return JSONObject.fromObject(BaseConsts.RESPONSE_MAP_ERROR).toString();
				}
			}
			
		}
		
		//如果是默认，则取消其他默认值
		if(DictionaryConsts.T_DICTIONART_ITEM_ISDEFAULT.equals( dictionaryItem.getIsDefault().toString())){
			dictionaryItemService.updateCancelAllDefalutItem(dictionaryItem.getDictionaryId());
		}
		
    	if(dictionaryItem.getId() == null){
    		//新增
    		
    		dictionaryItem.setCreateBy( ControllerUtil.getUser(request).getWorkcode());
    		dictionaryItem.setCreateTime(new Date());
    		dictionaryItemService.saveSelective(dictionaryItem);	
    	}else{
    		//修改
    		//如果是默认，则取消其他默认值
    		if(DictionaryConsts.T_DICTIONART_ITEM_ISDEFAULT.equals( dictionaryItem.getIsDefault().toString())){
    			dictionaryItemService.updateCancelAllDefalutItem(dictionaryItem.getDictionaryId());
    		}
    		
    		dictionaryItem.setUpdateBy(ControllerUtil.getUser(request).getWorkcode());
    		dictionaryItem.setUpdateTime(new Date());
    		dictionaryItemService.updateByPrimaryKeySelective(dictionaryItem);
    	}
    	
    	return  ControllerUtil.getJson(dictionaryItem);
    }
    
    /**
	 * 进入字典明细编辑页面
	 * @return
	 */
    @RequestMapping( value = "toEditPage" ,produces =RequestConsts.PRODUCES)
    public String toEditPage(HttpServletRequest request,Integer dictionaryItemId,Model model) {
    	if(dictionaryItemId !=null){
    		DictionaryItem  dictionaryItem = dictionaryItemService.getByPrimaryKey(dictionaryItemId);
    		model.addAttribute("dictionaryItem", dictionaryItem);
    		model.addAttribute(BaseConsts.LANGUAGE, ControllerUtil.getLanguage(request));
    	}
    	return "/dict/dictionaryItemedit";
    }
    
    /**
     * 进入字典明细创建页面
     * @param request
     * @param dictionaryId
     * @param model
     * @return
     */
    @RequestMapping( value = "toCreatePage" ,produces =RequestConsts.PRODUCES)
    public String toCreatePage(HttpServletRequest request,Integer dictionaryId,Model model) {
    		DictionaryItem  dictionaryItem = new DictionaryItem();
    		dictionaryItem.setDictionaryId(dictionaryId);
    		model.addAttribute("dictionaryItem", dictionaryItem);
    	return "/dict/dictionaryItemedit";
    }	
    
    @RequestMapping( value = "delete" ,produces =RequestConsts.PRODUCES)
    @ResponseBody
    public String delete(@RequestParam Integer dictionaryItemId) {
    	//用自定义的删除
    	dictionaryItemService.deleteByPrimaryKey(dictionaryItemId);
    	return ControllerUtil.getJson(BaseConsts.RESPONSE_MAP_SUCCESS);
    }

}
